/* 
 * Author: vdaras
 */

#include "gui_Screen.h"

#include "../MouseEvent.h"
#include "../Utilities.h"
#include <algorithm>

namespace gui
{

/**
 * Default constructor initializes the actual screen pointer.
 *
 * @param screen: the game's screen
 */

Screen::Screen(sdl::Canvas *screen)
:
Container("Screen", 0, 0, screen->GetWidth( ), screen->GetHeight( )),
m_gameScreen(screen)
{
    SDL_EnableUNICODE(SDL_ENABLE);
    //SDL_EnableKeyRepeat(500, 30);
    
    m_focusServer = new FocusServer;
}



Screen::~Screen()
{
    SDL_EnableUNICODE(SDL_DISABLE);
    
    delete m_focusServer;
}


/**
 * Screen container should not be resized by any mean.
 *
 * @param width:
 * @param height:
 */

void Screen::Resize(int width, int height)
{
    //cannot resize screen container, leave empty.
}


/**
 * Draws the gui's contents on the game's screen.
 */

void Screen::Draw() const
{
    Container::Draw( m_gameScreen );  
}


/**
 * Forwards the produced mouse event to the focus server.
 *
 * @param event: a mouse event occurred.
 */

void Screen::OnMouseEvent(MouseEvent &event)
{
    //if the event is a mouse movement then all the composite tree of the gui must be made aware of it.
    if( event.GetType( ) == MOUSE_MOTION )
    {
        Container::OnMouseEvent( event );
    }
    else if( event.GetType( ) == MOUSE_CLICKED && event.GetButtonUsed( ) == MBTN_LEFT )
    {
	Component *focused = m_focusServer->ResolveMouseFocus( event, this );
	
	if( focused )
	    focused->OnMouseEvent( event );
    }
    else if( event.GetType( ) == MOUSE_RELEASED && event.GetButtonUsed( ) == MBTN_LEFT )
    {
	Component *lastFocused = m_focusServer->GetLastFocused( );
	
	if( lastFocused )
	    lastFocused->OnMouseEvent( event );
    }
}


};

